﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcJE.Core.Inet;

namespace MvcJE.Web.Areas.Inet.Controllers
{
    public class TemplateGroupAdminController : Controller
    {
        private IPServiceDb db = new IPServiceDb();

        //
        // GET: /Inet/TemplateGroupAdmin/

        public ViewResult Index(int start = 0, int itemsPerPage = 20, string orderBy = "MopModuleGroupId", bool desc = false)
        {
            ViewBag.Count = db.MopModuleGroup.Count();
            ViewBag.Start = start;
            ViewBag.ItemsPerPage = itemsPerPage;
            ViewBag.OrderBy = orderBy;
            ViewBag.Desc = desc;

            return View();
        }

        //
        // GET: /Inet/TemplateGroupAdmin/GridData/?start=0&itemsPerPage=20&orderBy=MopModuleGroupId&desc=true

        public ActionResult GridData(int start = 0, int itemsPerPage = 20, string orderBy = "MopModuleGroupId", bool desc = false)
        {
            Response.AppendHeader("X-Total-Row-Count", db.MopModuleGroup.Count().ToString());
            ObjectQuery<MopModuleGroup> servicecategory = (db as IObjectContextAdapter).ObjectContext.CreateObjectSet<MopModuleGroup>();
            servicecategory = servicecategory.OrderBy("it." + orderBy + (desc ? " desc" : ""));

            return PartialView(servicecategory.Skip(start).Take(itemsPerPage));
        }

        //
        // GET: /Default5/RowData/5

        public ActionResult RowData(Guid id)
        {
            MopModuleGroup mopmodulegroup = db.MopModuleGroup.Find(id);
            return PartialView("GridData", new MopModuleGroup[] { mopmodulegroup });
        }

        //
        // GET: /Inet/TemplateGroupAdmin/Create

        public ActionResult Create()
        {
            return PartialView("Edit");
        }

        //
        // POST: /Inet/TemplateGroupAdmin/Create

        [HttpPost]
        public ActionResult Create(MopModuleGroup mopmodulegroup)
        {
            if (ModelState.IsValid)
            {
                // these two lines need to be a selection in future updates instead of statically assigning it
                var mopcatId = db.MopModuleCategory.Take(1).SingleOrDefault().MopModuleCategoryId;
                mopmodulegroup.MopModuleCategoryId = mopcatId;

                mopmodulegroup.MopModuleGroupId = Guid.NewGuid();
                db.MopModuleGroup.Add(mopmodulegroup);
                db.SaveChanges();
                return PartialView("GridData", new MopModuleGroup[] { mopmodulegroup });
            }

            return PartialView("Edit", mopmodulegroup);
        }

        //
        // GET: /Inet/TemplateGroupAdmin/Edit/5

        public ActionResult Edit(Guid id)
        {
            MopModuleGroup mopmodulegroup = db.MopModuleGroup.Find(id);
            return PartialView(mopmodulegroup);
        }

        //
        // POST: /Inet/TemplateGroupAdmin/Edit/5

        [HttpPost]
        public ActionResult Edit(MopModuleGroup mopmodulegroup)
        {
            if (ModelState.IsValid)
            {
                db.Entry(mopmodulegroup).State = EntityState.Modified;
                db.SaveChanges();
                return PartialView("GridData", new MopModuleGroup[] { mopmodulegroup });
            }

            return PartialView(mopmodulegroup);
        }

        //
        // POST: /Inet/TemplateGroupAdmin/Delete/5

        [HttpPost]
        public void Delete(Guid id)
        {
            MopModuleGroup mopmodulegroup = db.MopModuleGroup.Find(id);
            db.MopModuleGroup.Remove(mopmodulegroup);
            db.SaveChanges();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
